package com.ant.myagile.backlog
import grails.converters.JSON
import grails.plugins.springsecurity.Secured

import java.text.DateFormat
import java.text.SimpleDateFormat

import com.ant.myagile.Member
import com.ant.myagile.MemberService
import com.ant.myagile.SprintService
import com.ant.myagile.Team
import com.ant.myagile.TeamService;
import com.ant.myagile.dashboard.Status


class SprintController {
	
	def teamService
	def SpringSecurityService
	SprintService sprintService
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	MemberService memberService;

	@Secured(['ROLE_USER', 'ROLE_ADMIN', 'ROLE_GUEST'])
	def index(){
		Member memberlog= SpringSecurityService.currentUser
		def teams = teamService.getAllTeamByUser(memberlog.id)
		[teamList:teams]
	}

	def listSprintOfTeam(){
		Team team = Team.get(params.id)
		Sprint[] sprints = team.sprints
		render([sprintList:sprints] as JSON)
	}

	def saveSprint(){
		try {
			Sprint sprint= new Sprint()
			Team team = Team.get(params.teamid)
			sprint.team = team
			SimpleDateFormat formatDate = new SimpleDateFormat('yyyy-MM-dd')
			Date date1d =  formatDate.parse(params.dateStart)
			Date date2d =  formatDate.parse(params.dateEnd)
			sprint.team = team
			sprint.status=params.status
			sprint.sprintName=params.sprintName
			sprint.dateStart=date1d
			sprint.dateEnd=date2d
			sprint.save()
			def toDo = new Status(name: "To do",color:'#368EE0', position: 1, hidden: false, start: true, done: false, team: team, sprint: sprint)
			def done = new Status(name: "Done",color:'#368EE0', position: 2, hidden: false, start: false, done: true, team: team, sprint: sprint)
			toDo.save()
			done.save()
		}catch(Exception e){
			e.printStackTrace();
		}
		render ""
	}
	
def checkSprint(){
		    def check
			Sprint sprint= new Sprint()
			DateFormat formatDate = new SimpleDateFormat('yyyy-MM-dd')
			Date dateStart = formatDate.parse(params.dateStart)
			def dateEndQuery = Sprint.executeQuery("select max(dateEnd) from Sprint where team="+params.teamid)
			def dateEndStr = dateEndQuery[0].format("yyyy-MM-dd")
			Date dEnd = formatDate.parse(dateEndQuery[0].format("yyyy-MM-dd"));
			if(dateStart.compareTo(dEnd)>0){
				params.check = true
			}else{
				params.check = false
			}
			render params as JSON
	}

def checkSprintEdit(){
	def check
	Sprint sprint= new Sprint()
	DateFormat formatDate = new SimpleDateFormat('yyyy-MM-dd')
	Date dateStart = formatDate.parse(params.dateStart)
	def dateEndQuery = Sprint.executeQuery("select max(dateEnd) from Sprint where team="+params.teamid+" and id<>"+params.sprintID)
	def dateEndStr = dateEndQuery[0].format("yyyy-MM-dd")
	Date dEnd = formatDate.parse(dateEndQuery[0].format("yyyy-MM-dd"));
	
	println "ngay cuoi ke tiep"+dEnd
	if(dateStart.compareTo(dEnd)>0){
		params.check = true
	}else{
		params.check = false
	}
	render params as JSON
}
	
	def list() {
		Member loggedInUser = memberService.findLoggedInMember();
		[projects : loggedInUser.projects]
	}
	
	def showEdit(){
		def sprint =Sprint.get(params.sprintID)
		render (sprint as JSON)
	}
	
	def saveEditSprint(){
		def sprint= Sprint.get(params.sprintID)
		SimpleDateFormat formatDate = new SimpleDateFormat('yyyy-MM-dd')
		Date date1d = formatDate.parse(params.dateStart)
		Date date2d = formatDate.parse(params.dateEnd)
		sprint.sprintName = params.sprintName
		sprint.status = params.status
		sprint.dateStart = date1d
		sprint.dateEnd = date2d
		sprint.save()
		render ""
	}
	
	def jsonFindSprintByTeamId() {
		def sprints = sprintService.findSprintByTeamId(request.getParameter("teamID"));
		if(sprints) {
			render(contentType: 'text/json', {[sprints : sprints, result : 'OK']})
			response.status = 200
		} else {
			render(contentType: 'text/json', {[result : 'No Sprint']})
			response.status = 200
		}
	}
	
	def jsonFindSprintById()
	{
		def sprint = Sprint.findById(request.getParameter("sprintID"));
		if(sprint) {
			render(contentType: 'text/json', {[sprint : sprint, result : 'OK']})
			response.status = 200
		} else {
			render(contentType: 'text/json', {[result : 'No Sprint']})
			response.status = 200
		}
	}

}
